package com.citcc.api.netty.server;

import org.jboss.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelMap {

	public static int channelNum=0;
    public static Map<String,Channel> channelHashMap=null;//应改为concurrentHashmap以解决多线程冲突

    public static Map<String, Channel> getChannelHashMap() {
        return channelHashMap;
    }

    public static Channel getChannelByName(String name){
        if(channelHashMap==null||channelHashMap.isEmpty()){
            return null;
        }
        return channelHashMap.get(name);
    }
    public static void addChannel(String name,Channel channel){
        if(channelHashMap==null){
            channelHashMap=new ConcurrentHashMap<>(100);
        }
        channelHashMap.put(name,channel);
        //channelNum++;
    }
    public static int removeChannelByName(String name){
        if(channelHashMap.containsKey(name)){
            channelHashMap.remove(name);
            return 0;
        }else{
            return 1;
        }
    }
    
    public static int getNum(){
    	return channelHashMap.size();
    }
    
    public static void main(String[] args) {
    	if(ChannelMap.channelHashMap !=null){
    		System.out.println(getNum());
    	}else{
    		System.out.println(0);
    	}
		
	}
    
}
